package com.wcgksoft.controller.dataConfiguration;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.MsgInfo;
import com.wcgksoft.entity.Equipment.DataType;
import com.wcgksoft.entity.Equipment.Equipment;
import com.wcgksoft.entity.dataAnalysis.DepartProjectData;
import com.wcgksoft.entity.dataAnalysis.Threshold;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.DataAnalysis.DepartProjectDataService;
import com.wcgksoft.service.DataAnalysis.ThresholdService;
import com.wcgksoft.service.EnterpriseProjectService;
import com.wcgksoft.service.Equipment.DataTypeService;
import com.wcgksoft.service.Equipment.EquipmentService;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @ClassName DataController
 * @Description 加载数据配置
 * @Author user
 * @Date 2020/3/9 13:29
 * @Version V1.0
 **/

@Controller
@RequestMapping("system/dataConfig")
public class DataConfigController {

    @Autowired
    private DataTypeService dataTypeService;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private DepartProjectDataService departProjectDataService;

    @Autowired
    private EnterpriseProjectService enterpriseProjectService;

    @Autowired
    private ThresholdService thresholdService;

    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(@RequestParam(value = "eid") Integer eid) {
        DetachedCriteria criteria = DetachedCriteria.forClass(DataType.class);
        criteria.add(Restrictions.eq("e_id", eid));
        List<DataType> byConditions = dataTypeService.findByConditions(criteria);
        return Layui.data(0, "查询成功", byConditions);
    }
    @RequestMapping(value = "list")
    public String list(@RequestParam(value = "project") Integer project, HttpServletRequest req) {
        EnterpriseProject id1 = enterpriseProjectService.findById(project);
        req.getSession().setAttribute("project", id1);

        return "dataConfiguration/list";
    }


    @ResponseBody
    @RequestMapping(value = "findEquip")
    public Layui findEquip(@RequestParam(value = "eid") Integer eid) {
        DetachedCriteria criteria = DetachedCriteria.forClass(Equipment.class);
        criteria.add(Restrictions.eq("e_id", eid));
        List<Equipment> byConditions = equipmentService.findByConditions(criteria);
        return Layui.data(0, "查询成功", equipmentService.getTotalCount(), byConditions);
    }

    @ResponseBody
    @RequestMapping(value = "save")
    @Transactional
    public Layui saveOrUpdate(@RequestParam(value = "data",required = false)String data,
                              @RequestParam(value = "ensid",required = false)Integer ensid,
                              @RequestParam(value = "bbbbid",required = false)Integer bbbbid){
        JSONObject param = JSONObject.fromObject(data);
        if (param == null)
        {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            //EnterpriseProject enterpriseProject = (EnterpriseProject) SecurityUtils.getSubject().getSession().getAttribute("project");
            String anInt = param.getString("id");
            DepartProjectData departProjectData;
            Threshold threshold;
            if(null!=anInt&&!anInt.equals("")){
                 departProjectData = departProjectDataService.findById(Integer.valueOf(anInt));
                 DetachedCriteria detachedCriteria =DetachedCriteria.forClass(Threshold.class);
                detachedCriteria.add(Restrictions.eq("departProjectData.id",Integer.valueOf(anInt)));
                 threshold = thresholdService.findByConditions(detachedCriteria).get(0);

            }else{
                departProjectData = new DepartProjectData();
                threshold  = new Threshold();
                departProjectData.setEnterpriseProject(enterpriseProjectService.findById(ensid));
            }
            departProjectData.setBbbbid(bbbbid);
            departProjectData.setCreateDate(new Date());
            String eid=(String)param.get("equipment");
            Integer eids=Integer.parseInt(eid);
            departProjectData.setEquipment(equipmentService.findById(eids));
            String dataTypes=(String)param.get("dataType");
            Integer dataType=Integer.parseInt(dataTypes);
            departProjectData.setDataType(dataTypeService.findById(dataType));
            String dataNames=(String)param.get("dataName");
            departProjectData.setDataName(dataNames);
            String com=(String)param.get("com");
            departProjectData.setCom(com);
            departProjectData.setName(param.getString("name"));
            String upperlimitcolor=(String)param.get("upperlimitcolor");
            departProjectData.setUpperlimitcolor(upperlimitcolor);
            String lowerlimitcolor=(String)param.get("lowerlimitcolor");
            departProjectData.setLowerlimitcolor(lowerlimitcolor);
            Double upperlimit=param.getDouble("upperlimit");
            departProjectData.setUpperlimit(upperlimit);
            Double lowerlimit=param.getDouble("lowerlimit");
            departProjectData.setLowerlimit(lowerlimit);
            String phone01=param.getString("phone01");
            departProjectData.setPhone01(phone01);
            String phone02=param.getString("phone02");
            departProjectData.setPhone02(phone02);
            departProjectDataService.saveOrUpdate(departProjectData);


            threshold.setDepartProjectData(departProjectData);
            //告警
            String gjfwyz1=param.getString("gjfwyz");
            threshold.setGjfwyz(gjfwyz1);
            String gjfw1=param.getString("gjfw");
            double v = Double.parseDouble(gjfw1);
            threshold.setGjfw(v);
            //轻微超标
            String s =  param.getString("qwcbyz");
            threshold.setQwcbyz(s);
            String qwcb = param.getString("qwcb");
            double aDouble = Double.parseDouble(qwcb);
            threshold.setQwcb(aDouble);
            //正常
         /*   String s1=(String)param.get("zcfwyz");
            threshold.setZcfwyz(s1);
            String s3=(String)param.get("zcfw");
            double parseDouble = Double.parseDouble(s3);
            threshold.setZcfw(parseDouble);*/
            thresholdService.saveOrUpdate(threshold);
            return Layui.data(0, "保存成功",1,null);
        }
    }

    @ResponseBody
    @RequestMapping(value = "getArea")
    public List<EnterpriseProject> getArea(@RequestParam(value = "departid") Integer departid) {
        DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseProject.class);
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        criteria.createAlias("enterprise", "enterprise");
        criteria.add(Restrictions.eq("enterprise.id", sysUser.getEnterprise().getId()));
        criteria.add(Restrictions.eq("parentId", departid));
        List<EnterpriseProject> list = enterpriseProjectService.findByConditions(criteria);
        for (EnterpriseProject enterpriseProject : list) {
            DetachedCriteria criteria1 = DetachedCriteria.forClass(DepartProjectData.class);
            criteria1.add(Restrictions.eq("enterpriseProject.id",enterpriseProject.getId()));
            List<DepartProjectData> byConditions = departProjectDataService.findByConditions(criteria1);
            for (DepartProjectData condition : byConditions) {
                condition.setEnterpriseProject(null);
            }
            enterpriseProject.setDepartProjectData(byConditions);
        }
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "update")
    public DepartProjectData update(@RequestParam(value = "id") Integer id) {

        DepartProjectData id1 = departProjectDataService.findById(id);
        DetachedCriteria criteria = DetachedCriteria.forClass(Threshold.class);
        criteria.add(Restrictions.eq("departProjectData.id",id1.getId()));
        List<Threshold> list = thresholdService.findByConditions(criteria);
        list.get(0).setDepartProjectData(null);
        id1.setThreshold(list.get(0));
        return id1;
    }
    @ResponseBody
    @RequestMapping(value = "delent")
    public MsgInfo delent(@RequestParam(value = "id") Integer id) {

        DepartProjectData id1 = departProjectDataService.findById(id);
        DetachedCriteria criteria = DetachedCriteria.forClass(Threshold.class);
        criteria.add(Restrictions.eq("departProjectData.id",id1.getId()));
        List<Threshold> list = thresholdService.findByConditions(criteria);
        for (Threshold threshold : list) {
            thresholdService.delete(threshold);
        }
        departProjectDataService.delete(id1);
        MsgInfo info = new MsgInfo();
        info.setCode(200);
        return info;
    }
}
